package com.caixy.adminSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caixy.adminSystem.common.BaseResponse;
import com.caixy.adminSystem.common.ErrorCode;
import com.caixy.adminSystem.common.ResultUtils;
import com.caixy.adminSystem.exception.BusinessException;
import com.caixy.adminSystem.model.dto.filecategory.DeleteFileCategoryRequest;
import com.caixy.adminSystem.model.dto.filecategory.SelectFileCategoryRequest;
import com.caixy.adminSystem.model.dto.filecategory.UpdateFileCategoryRequest;
import com.caixy.adminSystem.model.dto.filetype.AddFileTypeRequest;
import com.caixy.adminSystem.model.dto.filetype.DeleteFileTypeRequest;
import com.caixy.adminSystem.model.dto.filetype.SelectFileTypeRequest;
import com.caixy.adminSystem.model.dto.filetype.UpdateFileTypeRequest;
import com.caixy.adminSystem.model.entity.FileCategory;
import com.caixy.adminSystem.model.entity.FileType;
import com.caixy.adminSystem.model.entity.User;
import com.caixy.adminSystem.service.FileInfoService;
import com.caixy.adminSystem.service.FileTypeService;
import com.caixy.adminSystem.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/FileType")
public class FileTypeController {
    @Resource
    private UserService userService;
    @Resource
    private FileTypeService fileTypeService;

    @Resource
    private FileInfoService fileInfoService;
    @PostMapping("/add")
    public BaseResponse<String> AddFileTypeComtroller(@RequestBody AddFileTypeRequest addFileTypeRequest, HttpServletRequest httpServletRequest){
        if (addFileTypeRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginer= userService.getLoginUser(httpServletRequest);
        addFileTypeRequest.setCreatorId(loginer.getId());
        FileType fileType = new FileType();
        BeanUtils.copyProperties(addFileTypeRequest,fileType);
        fileTypeService.check(fileType,true);
        fileTypeService.save(fileType);
        return ResultUtils.success("");
    }
    @PostMapping("/delete")
    public BaseResponse<String> DeleteFileTypeComtroller(@RequestBody DeleteFileTypeRequest deleteFileTypeRequest, HttpServletRequest httpServletRequest) {
        if (deleteFileTypeRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginer = userService.getLoginUser(httpServletRequest);
        deleteFileTypeRequest.setCreatorId(loginer.getId());
        FileType fileType = new FileType();
        BeanUtils.copyProperties(deleteFileTypeRequest, fileType);

        if (fileInfoService.CountFileInfoByTypeId(fileType.getId())>0){
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }else {
            fileTypeService.removeById(fileType);
        }


        return ResultUtils.success("");
    }
    @PostMapping("/update")
    public BaseResponse<String> UpdateFileTypeComtroller(@RequestBody UpdateFileTypeRequest updateFileTypeRequest, HttpServletRequest httpServletRequest){
        if (updateFileTypeRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginer= userService.getLoginUser(httpServletRequest);
        updateFileTypeRequest.setCreatorId(loginer.getId());
        FileType fileType = new FileType();
        BeanUtils.copyProperties(updateFileTypeRequest,fileType);
        fileTypeService.check(fileType,true);
        fileTypeService.updateById(fileType);
        return ResultUtils.success("");
    }
    @PostMapping("/select")
    public BaseResponse<String> SelectFileTypeComtroller(@RequestBody SelectFileTypeRequest selectFileTypeRequest, HttpServletRequest httpServletRequest){
        if (selectFileTypeRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginer= userService.getLoginUser(httpServletRequest);
        selectFileTypeRequest.setCreatorId(loginer.getId());
        FileType fileType = new FileType();
        BeanUtils.copyProperties(selectFileTypeRequest,fileType);

        fileTypeService.getById(fileType);

        return ResultUtils.success("");
    }

}
